/*++

Copyright (c) 2007  Microsoft Corporation 

Filename: 

    StatusDlg.cs

Abstract: 

    A form showing the status of Monitoring
*/

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using Visio = Microsoft.Office.Interop.Visio;

namespace NRTMAddIn
{
	/// <summary>
	/// Summary description for StatusDlg.
	/// </summary>
	public class StatusDlg : BaseForm
	{
		#region privates

        private string carriageReturn = "\r\n";

		#endregion

		#region controls

		private System.Windows.Forms.Button buttonCancel;

		#endregion

		#region fields
		
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.RichTextBox textBoxStatus;
        
        //Delegates to make code thread safe
        delegate void ResetStatusCallback();
        delegate void UpdateStatusCallback(string text);
        delegate void CancelButtonEnabledCallback(bool cancelcheck);

        private Document m_nrtmDocument;

		#endregion

		#region construction / destruction

		/// <summary>
		/// Default Constructor.
		/// </summary>
		public StatusDlg()
		{
			// Required for Windows Form Designer support
			InitializeComponent();
		}


		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#endregion

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(StatusDlg));
            this.buttonCancel = new System.Windows.Forms.Button();
            this.textBoxStatus = new System.Windows.Forms.RichTextBox();
            this.SuspendLayout();
            // 
            // buttonCancel
            // 
            resources.ApplyResources(this.buttonCancel, "buttonCancel");
            this.buttonCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.buttonCancel.Name = "buttonCancel";
            this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click);
            // 
            // textBoxStatus
            // 
            resources.ApplyResources(this.textBoxStatus, "textBoxStatus");
            this.textBoxStatus.Name = "textBoxStatus";
            this.textBoxStatus.ReadOnly = true;
            // 
            // StatusDlg
            // 
            resources.ApplyResources(this, "$this");
            this.Controls.Add(this.textBoxStatus);
            this.Controls.Add(this.buttonCancel);
            this.Name = "StatusDlg";
            this.ResumeLayout(false);

		}
		#endregion

		#region methods

		/// <summary>
		/// Clears all the items from the listbox.
		/// </summary>
        internal void ResetStatus()
        {
            // InvokeRequired required compares the thread ID of the
            // calling thread to the thread ID of the creating thread.
            // If these threads are different, it returns true.
            if (textBoxStatus.InvokeRequired)
            {
                ResetStatusCallback reset = new ResetStatusCallback(ResetStatus);
                this.Invoke(reset);
            }
            else
            {
                // reset the textbox
                textBoxStatus.Text = String.Empty;
            }

            // default return
            return;
        }


		/// <summary>
		/// Add this string to the status listbox.
		/// </summary>
		/// <param name="statusItem">
		/// The string to add to the listbox.
		/// </param>
        internal void UpdateStatus(
            string statusItem)
        {
            // InvokeRequired required compares the thread ID of the
            // calling thread to the thread ID of the creating thread.
            // If these threads are different, it returns true.
            if (textBoxStatus.InvokeRequired)
            {
                UpdateStatusCallback update = new UpdateStatusCallback(UpdateStatus);
                this.Invoke(update, new object[] { statusItem });
            }
            else
            {
                // add the item
                textBoxStatus.AppendText(carriageReturn + statusItem);
            }

            // default return
            return;
        }

        /// <summary>
        /// Enables Cancel Button 
        /// </summary>
        /// <param name="setValue"></param>
        internal void CancelButtonEnabled(bool setValue)
        {
            if (buttonCancel.InvokeRequired)
            {
                CancelButtonEnabledCallback setcancel = new CancelButtonEnabledCallback(CancelButtonEnabled);
                this.Invoke(setcancel, new object[] { setValue });
            }
            else
            {
                CancelEnabled = setValue;
            }
        }

        #endregion

		#region properties

		/// <summary>
		/// Set or return the Enabled property of the Cancel button on this form.
		/// </summary>
		internal bool CancelEnabled
		{
            get
            {
                return buttonCancel.Enabled;
            }
            set
            {
                buttonCancel.Enabled = value;
            }  
		}


		/// <summary>
		/// True - cancel button was pressed.
		/// False - cancel button was not pressed.
		/// </summary>
		internal DialogResult CancelPressed
		{
			get { return this.DialogResult; }
		}


		/// <summary>
		/// Gets or sets the NRTM Document object that this dialog is loaded for.
		/// </summary>
        internal Document NRTMDocument
        {
            get { return m_nrtmDocument; }

            set { m_nrtmDocument = value; }
        }

		#endregion

		#region control events

		/// <summary>
		/// Handler for the click event on the Cancel button.
		/// </summary>
		/// <param name="sender">
		/// </param>
		/// <param name="e">
		/// </param>
		private void buttonCancel_Click(
			object sender,
			System.EventArgs e)
		{
			try
			{
                if (m_nrtmDocument.diskMonitoringOn)
                {
                    //Stop monitoring
                    m_nrtmDocument.StopDiskMonitoring(Global.ThreadStop.Cancel);
                }
                if (m_nrtmDocument.diagramGenerationOn)
                {
                    //Stop diagram generation
                    m_nrtmDocument.StopDiagramGeneration(Global.ThreadStop.Cancel);
                }
			}
            catch (System.Threading.ThreadAbortException)
            {
                //Ignore thread abort exception
            }
            catch (NrtmException ex)
            {
                //To catch and display exceptions from sub functions
                NrtmException.HandleExceptions(Global.GetResourceString("Exception_MsgBox_Caption"),
                            Global.GetResourceString("Exception_UsrMsg"), ex.InnerException);
            }
            catch (Exception ex)
            {
                //To catch exceptions from current functions
                NrtmException.HandleExceptions(Global.GetResourceString("Exception_MsgBox_Caption"),
                            Global.GetResourceString("Exception_UsrMsg"), ex);
            }
            finally
			{
				// disable the button so it can only be pressed once.
				buttonCancel.Enabled = false;
			}

			// default return
			return;
		}

		#endregion       
	}
}
